package cn.mafangui.hotel.controller.user;

import cn.mafangui.hotel.entity.RoomType;
import cn.mafangui.hotel.entity.User;
import cn.mafangui.hotel.entity.UserVipLevel;
import cn.mafangui.hotel.mapper.UserVipLevelMapper;
import cn.mafangui.hotel.response.AjaxResult;
import cn.mafangui.hotel.response.ResponseTool;
import cn.mafangui.hotel.service.UserService;
import cn.mafangui.hotel.utils.MD5Utils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/user/vipLevel")
@CrossOrigin("*")
public class UserVipLevelController {

    @Resource
    private UserVipLevelMapper userVipLevelMapper;

    @Resource
    private UserService userService;

    /**
     * 所有等级
     * @return
     */
    @RequestMapping(value = "")
    public AjaxResult getAllRoomType(){
        List<UserVipLevel> all = userVipLevelMapper.findAll();
        return ResponseTool.success(all);
    }

    /**
     * 所有等级
     * @return
     */
    @PostMapping(value = "/pay")
    public AjaxResult sendPay(String money, String password, HttpServletRequest request){
        int userId = (int) request.getSession().getAttribute("userId");
        User user = userService.selectById(userId);
        if (user == null){
            return ResponseTool.failed("用户不存在");
        }
        if (!MD5Utils.MD5Encode(password).equals(user.getPaypassword())){
            return ResponseTool.failed("密码错误");
        }
        if (user.getPaymoney() != 0.0){
            return ResponseTool.failed("审核已提交，请勿重复提交!");
        }
        user.setPaymoney(Double.valueOf(money));
        if(userService.updateUser(user)==1)
            return ResponseTool.success("审核提交成功");
        return ResponseTool.failed("修改失败");
    }
}
